package hlhj.fhp.supreme.activitys

import android.annotation.SuppressLint
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import com.alipay.sdk.app.PayTask
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.Contents
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.*
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_chage_vip_aty.*
import kotlinx.android.synthetic.main.confirm_item.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ChageVipAty : BaseAty() {
    private lateinit var money:String
    private var num="1"
    private var payMoeny=0
    private var payType=2;//1支付宝2微信
    private var type=0//1-观点2课堂3直播
    private var id=0//项目id
    private lateinit var api: IWXAPI
    private val SDK_PAY_FLAG = 2
    private val halder= @SuppressLint("HandlerLeak")
    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            val result = msg!!.obj as Map<String, String>
            val status = result["resultStatus"]
            val ss = result["sub_msg"]
            LogUtil.logi("结果----$ss")
            if (status == "9000") {
                MyUtils.toast("支付成功")
                setResult(-1)
                val intent = Intent(this@ChageVipAty, PaySuccessAty::class.java)
                intent.putExtra("type","1")
                startActivity(intent)
                finish()
//                recreate()
            }
            else{
                val intent = Intent(this@ChageVipAty, PaySuccessAty::class.java)
                intent.putExtra("type","2")
                startActivity(intent)
                finish()
            }
            finish()
//                recreate()
        }
    }
    override fun getContentId(): Int {
        return R.layout.activity_chage_vip_aty
    }

    override fun initView() {
        EventBus.getDefault().register(this)
        tvPrice.text=intent.getStringExtra("money")
        num=intent.getStringExtra("num")
        api=WXAPIFactory.createWXAPI(this,Contents.WXAPPKEY)

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
        loZfb.setOnClickListener {
            payType=2
            cbZfb.isChecked=true
            cbWX.isChecked=false
        }

        loWx.setOnClickListener {
            payType=1
            cbZfb.isChecked=false
            cbWX.isChecked=true
        }
        btCommit.setOnClickListener {//等待支付宝
            when(payType){
                2->{
                    OkGo.post<AliPayBean>(Urls.BaseUrl+Urls.VIP_CHARGE)
                            .params("member",num)
                            .params("pay_type",payType)
                            .execute(object :JsonCallBack<AliPayBean>(this@ChageVipAty){
                                override fun onSuccess(response: Response<AliPayBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        val s = bean?.data.pay_data
                                        val runnable = Runnable {
                                            val alipay = PayTask(this@ChageVipAty)
                                            val result = alipay.payV2(s, true)
                                            val msg = Message()
                                            msg.what = SDK_PAY_FLAG
                                            msg.obj = result
                                            halder.sendMessage(msg)
                                        }
                                        val t = Thread(runnable)
                                        t.start()
                                    }
                                }
                            })
                }
                1->{
                    OkGo.post<WxPayBean>(Urls.BaseUrl+Urls.VIP_CHARGE)
                            .params("member",num)
                            .params("pay_type",payType)
                            .execute(object :JsonCallBack<WxPayBean>(this@ChageVipAty){
                                override fun onSuccess(response: Response<WxPayBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        val data = bean?.data
                                        val req = PayReq()
                                        req.appId=data.appid
                                        req.partnerId = data.partnerid
                                        req.prepayId = data.prepayid
                                        req.nonceStr = data.noncestr
                                        req.timeStamp = data.timestamp.toString()
                                        req.packageValue = data.packageX
                                        req.sign = data.sign
                                        req.extData="充值"
                                        api!!.sendReq(req)
                                    }
                                }
                            })
                }
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun handReadPay(event: ReadPayEvent){
        this@ChageVipAty.setResult(-1)
        val intent = Intent(this@ChageVipAty, PaySuccessAty::class.java)
        intent.putExtra("type","1")
        startActivity(intent)
        finish()
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun handPayFail(event: PayfailEvent){
        this@ChageVipAty.setResult(-1)
        val intent = Intent(this@ChageVipAty, PaySuccessAty::class.java)
        intent.putExtra("type","2")
        startActivity(intent)
        finish()
    }

}
